//
//  LGMedicineBallAddController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/12/8.
//  药球管理 -- 添加患者

#import "LGMedicineBallAddController.h"
#import "LGAddPatientSelectView.h"
#import "LGAddPatientInputView.h"
#import "LGPickerViewMax.h"
#import "DatePickerView.h"
@interface LGMedicineBallAddController ()<LGTextViewDelegate>
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)LGAddPatientSelectView *nameView;
/**   */
@property (nonatomic , strong)LGAddPatientSelectView *typeView;
/**   */
@property (nonatomic , strong)LGAddPatientSelectView *idTypeView;
/**   */
@property (nonatomic , strong)LGAddPatientSelectView *cardIDView;
/**   */
@property (nonatomic , strong)LGAddPatientSelectView *numberView;
/**   */
@property (nonatomic , strong)LGAddPatientSelectView *dateView;
/**   */
@property (nonatomic , strong)LGAddPatientInputView *remarkView;
/**   */
@property (nonatomic , strong)UIButton *saveBtn;
/**   */
@property (nonatomic , strong)NSArray *cardArray;
/**   */
@property (nonatomic , strong)NSArray *typeArray;


@end

@implementation LGMedicineBallAddController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationView.title = @"添加药球患者";
    [self createSubView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/api/common.dict/cardTypeOptions" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.cardArray = returnData;
        }
    } error:^(NSError *error) {
        
    }];
    
    [RequestUtil withPOST:@"/api/common.dict/patientHospitalPlanType" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.typeArray = returnData;
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)saveDataAction{
    NSString *cardID = self.cardIDView.contentTF.text;
    NSString *number = self.numberView.contentTF.text;
    
    if(self.nameView.contentTF.text.length == 0){
        [TooltipView showMessage:@"请输入患者姓名" offset:0];
        return;
    }
    if(self.typeView.contentTF.text.length == 0){
        [TooltipView showMessage:@"请选择入院类型" offset:0];
        return;
    }
    if(self.idTypeView.contentTF.text.length == 0){
        [TooltipView showMessage:@"请选择证件类型" offset:0];
        return;
    }
    if(cardID.length == 0){
        [TooltipView showMessage:@"请输入证件号" offset:0];
        return;
    }
    if(number.length == 0){
        [TooltipView showMessage:@"请输入登记号" offset:0];
        return;
    }
    if(self.dateView.contentTF.text.length == 0){
        [TooltipView showMessage:@"请选择计划入院日期" offset:0];
        return;
    }
    
    if([self.idTypeView.contentTF.text isEqualToString:@"身份证"] && [ObjectTool checkIDCardNumber:cardID] == NO){
        [TooltipView showMessage:@"身份证号不正确" offset:0];
        return;
    }
    
    if(number.length != 10){
        [TooltipView showMessage:@"登记号不正确" offset:0];
        return;
    }
    
    [RequestUtil withPOST:@"/doctor/patient.dcb.patient/addDcbPatient" parameters:@{@"realname":self.nameView.contentTF.text , @"type":self.typeView.content , @"card_type":self.idTypeView.content , @"card_no":self.cardIDView.contentTF.text , @"hospital_bano":self.numberView.contentTF.text , @"plan_date":self.dateView.contentTF.text ,  @"remark":self.remarkView.textView.content} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            if(self.addPatientSuccess){
                self.addPatientSuccess();
            }
            [TooltipView showMessage:@"保存成功" offset:0];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:@"保存失败，请重试" offset:0];
    }];
}

//选择入院类型
-(void)selectPlanTypeAction{
    [self.view endEditing:YES];
    if(self.typeArray.count > 0){
        NSMutableArray *tempArray = [NSMutableArray array];
        for (NSDictionary *tempDic in self.typeArray) {
            [tempArray addObject:LGNSString(tempDic[@"value"])];
        }
        
        LGPickerViewMax *pickerView = [[LGPickerViewMax alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        pickerView.pickArry = @[tempArray];
        pickerView.selectStr = self.typeView.contentTF.text;
        [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
        __weak typeof(self) weakSelf = self;
        pickerView.selectPickViewIndex = ^(NSArray *indexArray) {
            NSInteger index = [indexArray[0] integerValue];
            weakSelf.typeView.contentTF.text = LGNSString(weakSelf.typeArray[index][@"value"]);
            weakSelf.typeView.content = LGNSString(weakSelf.typeArray[index][@"key"]);
        };
        
    }else{
        [self requestData];
    }
}

//选择证件类型
-(void)selectCardTypeAction{
    [self.view endEditing:YES];
    if(self.cardArray.count > 0){
        NSMutableArray *tempArray = [NSMutableArray array];
        for (NSDictionary *tempDic in self.cardArray) {
            [tempArray addObject:LGNSString(tempDic[@"title"])];
        }
        
        LGPickerViewMax *pickerView = [[LGPickerViewMax alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        pickerView.pickArry = @[tempArray];
        pickerView.selectStr = self.idTypeView.contentTF.text;
        [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
        __weak typeof(self) weakSelf = self;
        pickerView.selectPickViewIndex = ^(NSArray *indexArray) {
            NSInteger index = [indexArray[0] integerValue];
            weakSelf.idTypeView.contentTF.text = LGNSString(weakSelf.cardArray[index][@"title"]);
            weakSelf.idTypeView.content = LGNSString(weakSelf.cardArray[index][@"value"]);
        };
    }else{
        [self requestData];
    }
}

//选择入院日期
-(void)selectPlanDateAction{
    [self.view endEditing:YES];
    DatePickerView *pickerView = [[DatePickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.currentDate = self.dateView.contentTF.text;
    [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
    __weak typeof(self) weakSelf = self;
    pickerView.selectDate = ^(NSString *date) {
        weakSelf.dateView.contentTF.text = date;
    };
}

-(void)textViewHeightChanged:(CGFloat)height{
    if(height>viewPix(67)){
        self.remarkView.frame = CGRectMake(viewPix(16), viewPix(16)+kMaxY(self.dateView.frame), Screen_W-viewPix(32), viewPix(54)+height);
    }
    self.scrollView.contentSize = CGSizeMake(Screen_W, kMaxY(self.remarkView.frame)+viewPix(10));
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.nameView];
    [self.scrollView addSubview:self.typeView];
    [self.scrollView addSubview:self.idTypeView];
    [self.scrollView addSubview:self.cardIDView];
    [self.scrollView addSubview:self.numberView];
    [self.scrollView addSubview:self.dateView];
    [self.scrollView addSubview:self.remarkView];
    [self.view addSubview:self.saveBtn];
    self.scrollView.contentSize = CGSizeMake(Screen_W, kMaxY(self.remarkView.frame)+viewPix(10));
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight-viewPix(65)-bottomSafeBarHeight/3.0)];
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.showsVerticalScrollIndicator = NO;
    }
    return _scrollView;
}

-(LGAddPatientSelectView *)nameView{
    if(!_nameView){
        _nameView = [[LGAddPatientSelectView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(54))];
        _nameView.title = @"姓名";
        _nameView.contentTF.placeholder = @"请输入";
        _nameView.showTip = NO;
        _nameView.maxNum = 10;
    }
    return _nameView;
}

-(LGAddPatientSelectView *)typeView{
    if(!_typeView){
        _typeView = [[LGAddPatientSelectView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(16)+kMaxY(self.nameView.frame), Screen_W-viewPix(32), viewPix(54))];
        _typeView.title = @"入院类型";
        _typeView.contentTF.placeholder = @"请选择";
        _typeView.showTip = YES;
        [_typeView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectPlanTypeAction)]];
    }
    return _typeView;
}

-(LGAddPatientSelectView *)idTypeView{
    if(!_idTypeView){
        _idTypeView = [[LGAddPatientSelectView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(16)+kMaxY(self.typeView.frame), Screen_W-viewPix(32), viewPix(54))];
        _idTypeView.title = @"证件类型";
        _idTypeView.contentTF.placeholder = @"请选择";
        _idTypeView.contentTF.text = @"身份证";
        _idTypeView.content = @"10";
        _idTypeView.showTip = YES;
        [_idTypeView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectCardTypeAction)]];
    }
    return _idTypeView;
}

-(LGAddPatientSelectView *)cardIDView{
    if(!_cardIDView){
        _cardIDView = [[LGAddPatientSelectView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(16)+kMaxY(self.idTypeView.frame), Screen_W-viewPix(32), viewPix(54))];
        _cardIDView.title = @"证件号";
        _cardIDView.contentTF.placeholder = @"请输入";
        _cardIDView.showTip = NO;
    }
    return _cardIDView;
}

-(LGAddPatientSelectView *)numberView{
    if(!_numberView){
        _numberView = [[LGAddPatientSelectView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(16)+kMaxY(self.cardIDView.frame), Screen_W-viewPix(32), viewPix(54))];
        _numberView.title = @"登记号";
        _numberView.contentTF.placeholder = @"请输入";
        _numberView.contentTF.keyboardType = UIKeyboardTypeNumberPad;
        _numberView.showTip = NO;
        _numberView.maxNum = 10;
    }
    return _numberView;
}

-(LGAddPatientSelectView *)dateView{
    if(!_dateView){
        _dateView = [[LGAddPatientSelectView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(16)+kMaxY(self.numberView.frame), Screen_W-viewPix(32), viewPix(54))];
        _dateView.title = @"计划入院日期";
        _dateView.contentTF.placeholder = @"请选择";
        _dateView.showTip = YES;
        [_dateView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectPlanDateAction)]];
    }
    return _dateView;
}

-(LGAddPatientInputView *)remarkView{
    if(!_remarkView){
        _remarkView = [[LGAddPatientInputView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(16)+kMaxY(self.dateView.frame), Screen_W-viewPix(32), viewPix(122))];
        _remarkView.textView.customDelegate = self;
    }
    return _remarkView;
}

-(UIButton *)saveBtn{
    if(!_saveBtn){
        _saveBtn = [UIButton buttonWithTitle:@"保存" topMargin:Screen_H-viewPix(60)-bottomSafeBarHeight/3.0];
        [_saveBtn addTarget:self action:@selector(saveDataAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
